import React, { useEffect, useRef, useState } from 'react';
import { Card, Modal, Button, Form, Image, message } from 'antd';
import { connect, useRequest } from "umi";
import { countryNo, city, area, prodtree } from '@/services/basic';
import InitForm from '@/components/InitForm';
import { doFetch } from '@/utils/doFetch';
import ProDescriptions from '@ant-design/pro-descriptions';


let defaultFields = {
    "contactName": {
        "value": null,
        "type": "input",
        "title": "联系人*",
        "name": [
            "contactName"
        ],
        "required": true
    },
    "genderType": {
        "value": null,
        "type": "select",
        "title": "性别",
        "name": [
            "genderType"
        ],
        "required": true,
        "options": [
            {
                "label": "男",
                "value": 1
            },
            {
                "label": "女",
                "value": 2
            }
        ]
    },
    "countryId": {
        "value": null,
        "type": "select",
        "title": "国际区号*",
        "name": [
            "countryId"
        ],
        "required": true,
        "options": {
            "database": countryNo,
            "params": {}
        }
    },
    "telephone": {
        "value": null,
        "type": "input",
        "title": "联系电话*",
        "name": [
            "telephone"
        ],
        "required": true
    },
    "areaNo": {
        "value": null,
        "type": "input",
        "title": "区号",
        "name": [
            "areaNo"
        ],
        "required": true
    },
    "specialPlane": {
        "value": null,
        "type": "input",
        "title": "座机",
        "name": [
            "specialPlane"
        ],
        "required": true
    },
    "mailNo": {
        "value": null,
        "type": "input",
        "title": "邮箱",
        "name": [
            "mailNo"
        ],
        "required": true
    },
    "jobTitle": {
        "value": null,
        "type": "input",
        "title": "职务",
        "name": [
            "jobTitle"
        ],
        "required": true
    },
    "departmentName": {
        "value": null,
        "type": "input",
        "title": "所属部门",
        "name": [
            "departmentName"
        ],
        "required": true
    }
}
let Company = (props) => {
    const { global, dispatch } = props;

    const actionRef = useRef();
    const [vs, cvs] = useState(false),//表单显/隐
        [fields, cf] = useState(defaultFields),
        [iftype, ciftype] = useState({}),
        [formRef] = Form.useForm(),
        [data, cdata] = useState({});


    const { run, loading } = useRequest(doFetch, {
        manual: true,
        formatResult: (res) => res,
        onSuccess: (result, params) => {
            message.destroy()
            if (result.code == "0000") {
                cdata(result.data.data);
                cvs(false);
            } else {
                message.error(result.msg)
            }
        }
    })
    useEffect(() => {
        run({ url: "/ngic-promote-company/omCompanyContactAudit/queryBeforeAddAudit", params: {} })
    }, [])

    const columns = [
        {
            "title": "联系人",
            "dataIndex": "contactName",
            "key": "contactName"
        },
        {
            "title": "性别",
            "dataIndex": "genderName",
            "key": "genderName"
        },
        {
            "title": "国际区号名",
            "dataIndex": "countryNo",
            "key": "countryNo"
        },
        {
            "title": "联系电话",
            "dataIndex": "telephone",
            "key": "telephone"
        },
        {
            "title": "区号",
            "dataIndex": "areaNo",
            "key": "areaNo"
        },
        {
            "title": "座机",
            "dataIndex": "specialPlane",
            "key": "specialPlane"
        },
        {
            "title": "邮箱",
            "dataIndex": "mailNo",
            "key": "mailNo"
        },
        {
            "title": "职务",
            "dataIndex": "jobTitle",
            "key": "jobTitle"
        },
        {
            "title": "所属部门",
            "dataIndex": "departmentName",
            "key": "departmentName"
        },
        {
            "title": "审核结果",
            "dataIndex": "auditResult",
            "key": "auditResult"
        },
        {
            "title": "审核状态名",
            "dataIndex": "statusName",
            "key": "statusName"
        }
    ]


    let saveData = (values, fn) => {
        let newfields = JSON.parse(JSON.stringify(values));
        //新增&修改

        doFetch({ url: "/ngic-promote-company/omCompanyContactAudit/save", params: { ...newfields } }).then(res => {
            if (res.code == "0000") {
                run({ url: "/ngic-promote-company/omCompanyContactAudit/queryBeforeAddAudit", params: {} })
                message.success("操作成功！")
            }
        })
    }



    return (
        <div className="nopa autotable">
            <Card title={"联系信息"} extra={<a onClick={() => {
                ciftype({
                    val: "edit",
                    id: data.id,
                    title: "编辑联系信息"
                })
                cf(() => {
                    for (let i in defaultFields) {
                        defaultFields[i].value = data[i];
                        if (i.indexOf("password") != -1) {
                            defaultFields[i].hides = true;
                        }
                    }
                    return { ...defaultFields }
                });
                cvs(true);
            }}>
                编辑
            </a>}>
                <div style={{ padding: "18px 12px 0px 12px" }} className="minw">
                    <ProDescriptions
                        title={``}
                        columns={[...columns]}
                        column={{ xs: 1, sm: 1, md: 1, lg: 1, xl: 1, xxl: 1 }}
                        dataSource={data}
                    />
                </div>

            </Card>



            <Modal
                maskClosable={false}
                title={iftype.title}
                visible={vs}
                onCancel={() => cvs(false)}
                footer={false}
                destroyOnClose={true}
            >
                <InitForm
                    formRef={formRef}
                    fields={fields}
                    submitData={(values, fn) => {
                        saveData(values, fn)
                    }}
                    onChange={(changedValues, allValues) => {
                    }}
                    submitting={
                        loading || !vs
                    }
                >
                </InitForm>
            </Modal>
        </div>
    )
}

export default connect(({ global, loading }) => ({
    global,
    loading,
}))(Company)